package com.wmm.jinhong;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HistoryActivity extends AppCompatActivity {
    @BindView(R.id.list)
    ListView listView;
    private HistoryAdapter adapter;
    private List<Bean> list=new ArrayList<>();
    private FragmentManager manager;
    Timer timer=null;
    private boolean cancle=true;
    TimerTask task;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);
        manager=getSupportFragmentManager();
        adapter=new HistoryAdapter(this,list,manager);
        listView.setAdapter(adapter);
        getList();
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);
            }
        };
        timer.schedule(task,0,30000);
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==1){
//                timer = new Timer();
//                task = new TimerTask() {
//                    @Override
//                    public void run() {
//                        getLists();
//                    }
//                };
//                timer.schedule(task,0,60000);
//                cancle=true;
//                getLists();
            }else{
                getList();
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
        timer=null;
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_HOME){
            Intent mIntent = new Intent();
            mIntent.setAction(Intent.ACTION_MAIN);
            mIntent.addCategory(Intent.CATEGORY_HOME);
            startActivity(mIntent);
        }
        return super.onKeyDown(keyCode, event);
    }
    private void getList(){
// 相当于 select * from students 语句
        Cursor cursor = MyApp.getInstance().query(SQLiteDbHelper.TABLE_STUDENT, null,
                "", null,
                null, null, null, null);
        list.clear();
        // 不断移动光标获取值
        while (cursor.moveToNext()) {
            // 直接通过索引获取字段值
            Bean bean=new Bean();
            bean.type=cursor.getString(0);
            bean.time=cursor.getString(2);
            bean.list=cursor.getString(1);
            bean.kehu=cursor.getString(4);
            bean.ydhao=cursor.getString(3);
            list.add(bean);
//            String stuName = cursor.getString(cursor.getColumnIndex("name"));
        }
        adapter.notifyDataSetChanged();
        // 关闭光标
        cursor.close();
    }
}
